package com.hangwei.paperless;

import android.app.ActivityManager;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Handler;

import com.facebook.common.memory.MemoryTrimType;
import com.facebook.common.memory.MemoryTrimmable;
import com.facebook.common.memory.MemoryTrimmableRegistry;
import com.facebook.common.memory.NoOpMemoryTrimmableRegistry;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.imagepipeline.backends.okhttp3.OkHttpImagePipelineConfigFactory;
import com.facebook.imagepipeline.core.ImagePipelineConfig;
import com.hangwei.paperless.model.bean.UserInfo;
import com.hangwei.paperless.utils.CrashHandler;
import com.hangwei.paperless.utils.LolipopBitmapMemoryCacheSupplier;
import com.twelve.all.base.NFCApp;
import com.twelve.all.utils.Tools;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import java.util.List;

import okhttp3.OkHttpClient;

/**
 * Created by 张龙臣 on 2020/10/18.
 */

public class App extends NFCApp {

    public static List<UserInfo.StampBean> stamps;
    public static List<UserInfo.DocroleBean> docrole;

    public static Handler getHandler() {
        return handler;
    }

    private static Handler handler;

    @Override
    public void onCreate() {
        super.onCreate();
        Tools.init(this);
        handler = new Handler();
        Fresco.initialize(this, getImagePipelineConfig(getApplicationContext()));
        UMConfigure.init(this, "5fe4694e44bb94418a63bcf6", "Umeng", UMConfigure.DEVICE_TYPE_PHONE, "");
        MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
        UMConfigure.setLogEnabled(true);
        CrashHandler.getInstance().init(this);
        //        CrashHandler.getInstance().init(getApplicationContext());
    }
    private ImagePipelineConfig getImagePipelineConfig(Context context) {
        // 当内存紧张时采取的措施
        MemoryTrimmableRegistry memoryTrimmableRegistry = NoOpMemoryTrimmableRegistry.getInstance();
        memoryTrimmableRegistry.registerMemoryTrimmable(new MemoryTrimmable() {
            @Override
            public void trim(MemoryTrimType trimType) {
                final double suggestedTrimRatio = trimType.getSuggestedTrimRatio();
                if (MemoryTrimType.OnCloseToDalvikHeapLimit.getSuggestedTrimRatio() == suggestedTrimRatio
                        || MemoryTrimType.OnSystemLowMemoryWhileAppInBackground.getSuggestedTrimRatio() == suggestedTrimRatio
                        || MemoryTrimType.OnSystemLowMemoryWhileAppInForeground.getSuggestedTrimRatio() == suggestedTrimRatio
                        ) {
                    // 清除内存缓存
                    Fresco.getImagePipeline().clearMemoryCaches();
                }
            }
        });
        OkHttpClient okHttpClient = new OkHttpClient.Builder().build();
        ImagePipelineConfig config = OkHttpImagePipelineConfigFactory.newBuilder(context, okHttpClient)
                .setBitmapMemoryCacheParamsSupplier(new LolipopBitmapMemoryCacheSupplier((ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE)))
                .setDownsampleEnabled(true)
                .setMemoryTrimmableRegistry(memoryTrimmableRegistry)
                .setBitmapsConfig(Bitmap.Config.RGB_565)
                .build();
        return config;
    }


}
